<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 供应商管理
 * @package app\erp\model
 */
class ErpSupplier extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_supplier';

    /**
     * 查询数据
     * @param id int 供应商id
     * @author jason
     * @return array_out(code, message, (int)id)
     */
    public function getInfoById($id, $cache = true){
        $info = $this -> cache($cache) -> find($id);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param title 	            string  供应商名称
     * @param code 		            string  供应商编码
     * @param settlement_cycle 		int     结算周日（天）
     * @param corporate_name 		string  公司名称
     * @param corporate_address 	string  公司地址
     * @param remarks 		        text    备注
     * @param sort 		            int     排序
	 * @param supplier_type 		int     1经销 2代销 3联营
     * @param tax_rate 				int     税率(%)
     * @param bank_account 			string  银行账户
     * @param tax_code 				string  税号
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($title, $settlement_cycle, $corporate_name, $corporate_address, $remarks, $sort, $supplier_type, $tax_rate, $bank_account, $tax_code, $jiesuan_type, $jiesuan_date, $rebate){
        $data['title']              = $title;
        $data['settlement_cycle']   = $settlement_cycle;
        $data['corporate_name']     = $corporate_name;
        $data['corporate_address']  = $corporate_address;
        $data['remarks']            = $remarks;
        $data['sort']               = $sort;
        $data['supplier_type']      = $supplier_type;
        $data['tax_rate']           = $tax_rate;
        $data['bank_account']       = $bank_account;
        $data['tax_code']           = $tax_code;
        $data['jiesuan_type']       = $jiesuan_type;
        $data['jiesuan_date']       = $jiesuan_date;
        $data['rebate']       		= $rebate;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	
	/**
     * 编辑数据
     * @param id 	            string  供应商名称
     * @param title 	            string  供应商名称
     * @param code 		            string  供应商编码
     * @param settlement_cycle 		int     结算周日（天）
     * @param corporate_name 		string  公司名称
     * @param corporate_address 	string  公司地址
     * @param remarks 		        text    备注
     * @param sort 		            int     排序
	 * @param supplier_type 		int     1经销 2代销 3联营
     * @param tax_rate 				int     税率(%)
     * @param bank_account 			string  银行账户
     * @param tax_code 				string  税号
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
	public function editData($id, $title, $settlement_cycle, $corporate_name, $corporate_address, $remarks, $sort, $supplier_type, $tax_rate, $bank_account, $tax_code, $jiesuan_type, $jiesuan_date, $rebate){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
		$data['title'] 		        = $title;
        $data['settlement_cycle'] 	= $settlement_cycle;
        $data['corporate_name'] 	= $corporate_name;
        $data['corporate_address'] 	= $corporate_address;
        $data['remarks'] 	        = $remarks;
        $data['sort'] 		        = $sort;
		$data['supplier_type']      = $supplier_type;
        $data['tax_rate']           = $tax_rate;
        $data['bank_account']       = $bank_account;
        $data['tax_code']           = $tax_code;
        $data['jiesuan_type']       = $jiesuan_type;
        $data['jiesuan_date']       = $jiesuan_date;
        $data['rebate']             = $rebate;

        $res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
